# @Author ZhangGJ
# @Date 2021/11/30 11:43

import cv2.cv2 as cv2


# 加权和
def add_weighted():
    sun = cv2.imread('../images/sunset.jpg')
    beach = cv2.imread('../images/beach.jpg')
    rows, columns, channel = sun.shape
    beach = cv2.resize(beach, (columns, rows))
    img = cv2.addWeighted(sun, 0.6, beach, 0.6, 0)
    cv2.imshow('sun', sun)
    cv2.imshow('beach', beach)
    cv2.imshow('addWeighted', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 覆盖
def override():
    beach_img = cv2.imread('../images/beach.jpg')
    cat_img = cv2.imread('../images/cat.jpg')
    cat = cat_img[75:400, 120:260, :]
    cat = cv2.resize(cat, (70, 160))
    cv2.imshow('cat', cat_img)
    cv2.imshow('cat2', cat)
    cv2.imshow('beach', beach_img)
    rows, columns, channel = cat.shape
    beach_img[100:100 + rows, 260:260 + columns, :] = cat
    cv2.imshow('beach2', beach_img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def no_smoking_image():
    smoking = cv2.imread('../images/smoking.png', cv2.IMREAD_UNCHANGED)
    no_img = cv2.imread('../images/no.png', cv2.IMREAD_UNCHANGED)
    cv2.imshow('no_img', no_img)
    cv2.imshow('smoking', smoking)
    img = _overlay_img(smoking, no_img, 95, 90)
    cv2.imshow('no smoking', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


def _overlay_img(img, img_over, img_over_x, img_over_y):
    img_h, img_w, img_p = img.shape
    img_over_h, img_over_w, img_over_c = img_over.shape
    if img_over_c <= 3:
        img_over = cv2.cvtColor(img_over, cv2.COLOR_BGR2BGRA)
    for w in range(0, img_over_h):
        for h in range(0, img_over_h):
            if img_over[h, w, 3] != 0:
                for c in range(0, 3):
                    x = img_over_x + w
                    y = img_over_y + h
                    if x >= img_w or y >= img_h:
                        break
                    img[y, x, c] = img_over[h, w, c]
    return img


if __name__ == '__main__':
    no_smoking_image()
